package com.dgshanger.sanhxiaofeisc.ui.person

import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentPagerAdapter
import android.support.v4.content.ContextCompat
import android.support.v4.view.ViewPager
import com.dgshanger.sanhxiaofeisc.R
import com.dgshanger.sanhxiaofeisc.entity.MyTeamEntity
import com.dgshanger.sanhxiaofeisc.mvp.contract.MyTeamContract
import com.dgshanger.sanhxiaofeisc.mvp.presenter.MyTeamPresenter
import com.xx.baseuilibrary.mvp.BaseMvpActivity
import kotlinx.android.synthetic.main.activity_my_team.*


/**
 * MyTeamActivity
 * 沉迷学习不能自拔
 * Describe：我的团队页面
 * Created by 雷小星🍀 on 2018/4/16 11:35.
 */
class MyTeamActivity : BaseMvpActivity<MyTeamContract.Model, MyTeamContract.View, MyTeamPresenter>(), MyTeamContract.View {

    override fun setUserDetail(userDetail: MyTeamEntity.UserDetailBean?) {
        with(userDetail!!) {
            tv_distribute_money.text = distribute_money
            tv_invite_user_total.text = invite_user_total
            tv_invite_shop_total.text = invite_shop_total
        }
    }

    override fun createPresenter(): MyTeamPresenter = MyTeamPresenter()

    override fun getActivityLayoutId(): Int = R.layout.activity_my_team

    override fun initData() {
        viewPager.adapter = Adapter(supportFragmentManager)
        getPresenter().loadDetail()
    }

    override fun initEvent() {
        tv_inviter_user.setOnClickListener {
            viewPager.setCurrentItem(0)

            tv_inviter_user.setTextColor(ContextCompat.getColor(mContext, R.color.colorBlack))
            tv_inviter_user.textSize = 20f

            tv_inviter.setTextColor(ContextCompat.getColor(mContext, R.color.colorText))
            tv_inviter.textSize = 16f
        }
        tv_inviter.setOnClickListener {
            viewPager.setCurrentItem(1)

            tv_inviter_user.setTextColor(ContextCompat.getColor(mContext, R.color.colorText))
            tv_inviter_user.textSize = 16f

            tv_inviter.setTextColor(ContextCompat.getColor(mContext, R.color.colorBlack))
            tv_inviter.textSize = 20f
        }

        viewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(state: Int) {
            }

            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
            }

            override fun onPageSelected(position: Int) {
                if (position == 0) {
                    tv_inviter_user.performClick()
                } else {
                    tv_inviter.performClick()
                }
            }
        })
    }


    inner class Adapter(fm: FragmentManager) : FragmentPagerAdapter(fm) {
        override fun getItem(position: Int): Fragment {
            when (position) {
                0 -> return MyTeamListFragment()
                1 -> return MyTeamInviterListFragment()
                else ->
                    return MyTeamListFragment()
            }
        }

        override fun getCount(): Int = 2

    }
}